package constant.milk.toilet.widget;

import java.util.Timer;
import java.util.TimerTask;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;

public class WidgetService extends Service
{
	private TimerTask task;    
	private Timer timer;
	
	@Override
	public void onCreate()
	{
		super.onCreate();
	}
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId)
	{
		task = new TimerTask() 
		{            
			@Override            
			public void run() 
			{            
				Intent intent = new Intent(ToiletWidget.TIMER);
				PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 
						0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
				
		        AlarmManager manager = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
		        manager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), pendingIntent);
			} 
		}; 
		
		timer = new Timer();                 
		timer.schedule(task, 0, 500);
		
		return START_STICKY;
	}
	
	@Override
	public void onDestroy()
	{
		if (timer != null)
		{
			timer.cancel();
			timer.purge();
			timer = null;
		}
		
		super.onDestroy();
	}
	
	@Override
	public IBinder onBind(Intent intent)
	{
		return null;
	}
}
